﻿using IOP.SgrA;
using IOP.SgrA.ECS;
using IOP.SgrA.SilkNet.Vulkan;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using IOP.Extension.DependencyInjection;
using IOP.ISEA;
using System.Linq;

namespace VkSample912
{
    [ModulePriority(ModulePriority.Assets)]
    public class AssetsModule : VulkanModule, IVulkanRenderWapperModule
    {
        public VulkanRenderWapper RenderWapper { get; set; }
        [Autowired]
        private ILogger<AssetsModule> _Logger { get; set; }

        protected override async Task Load(VulkanGraphicsManager graphicsManager)
        {
            try
            {
                var basePath = AppContext.BaseDirectory;
                var modelsPath = Path.Combine(basePath, "Assets", "Models");
                var lDevice = graphicsManager.VulkanDevice;

                var commonGroup = graphicsManager.TryGetRenderGroup("Common", out IRenderGroup group);

                ISEADocument document = new ISEADocument();
                await document.Load(Path.Combine(AppContext.BaseDirectory, "test912.isea"));
                var renders = await graphicsManager.CreateRenderObjectsFromISEA<RenderObject>(document);
                var ch = renders.Where((x) => x.Name == "Ch").First();
                var qt = renders.Where((x) => x.Name == "Qt").First();

                var archetype = ContextManager.CreateArchetype(new CommonComponent(), new TransformComponent());
                var lightArchetype = ContextManager.CreateArchetype(new LightComponent());
                ContextManager.CreateContexts(1, archetype, group, ch, new TransformGroup()
                {
                    Transform = new TransformComponent
                    {
                        Angle = Quaternion.Identity,
                        Scale = Vector3.One,
                        Position = new Vector3(15, 0, -25)
                    }
                });
                ContextManager.CreateContexts(1, archetype, group, ch, new TransformGroup()
                {
                    Transform = new TransformComponent
                    {
                        Angle = Quaternion.Identity,
                        Scale = Vector3.One,
                        Position = new Vector3(15, 0, 5)
                    }
                });
                ContextManager.CreateContexts(1, archetype, group, qt, new TransformGroup()
                {
                    Transform = new TransformComponent
                    {
                        Angle = Quaternion.Identity,
                        Scale = Vector3.One,
                        Position = new Vector3(-15, 0, 8)
                    }
                });
                ContextManager.CreateContexts(1, archetype, group, qt, new TransformGroup()
                {
                    Transform = new TransformComponent
                    {
                        Angle = Quaternion.Identity,
                        Scale = Vector3.One,
                        Position = new Vector3(-15, 3, -2)
                    }
                });
                var light = graphicsManager.CreatePointLight<RenderObject>("main", new Vector3(100, 100, 100), new LightStrength(0.9f, 0.9f, 0.9f, 1.0f));
                ContextManager.CreateContexts(1, lightArchetype, group, light);
            }
            catch (Exception e)
            {
                _Logger.LogError(e.Message + "\r\n" + e.StackTrace);
            }
        }

        protected override Task Unload(VulkanGraphicsManager manager)
        {
            return Task.CompletedTask;
        }
    }
}
